/**
 * @author 老刘
 * @desc 自定义数字键盘
 */
const app = getApp();
Page({
	data: {
		numberNoStr: ['', '', '', '', '', ''],
		showKeyboard: false, // 是否显示输入数字键盘
		currentIndex: -1, // 当前输入的位置
		numberNo: '',
		available: false // 按钮是否可点击
	},
	setCurrentCodeNo (e) {
		let index = e.currentTarget.dataset['index'];
		index = parseInt(index);
		let keyboard = this.selectComponent('#keyboard');
		keyboard.indexMethod(index, this.data.currentIndex);
		this.setData({
			currentIndex: index,
			showKeyboard: true
		});
	},
	// 输入验证码回调
	valueChange (e) {
		if (app.globalData.SDKVersion < '2.6.1') {
			let keyboard = this.selectComponent('#keyboard');
			keyboard.indexMethod(e.detail.index, this.data.currentIndex);
		}
		this.setData({
			numberNo: e.detail.numberNo,
			numberNoStr: e.detail.numberNo.join(''),
			currentIndex: e.detail.index,
			showKeyboard: e.detail.show
		});
		if (app.globalData.SDKVersion < '2.6.1') {
			let keyboard = this.selectComponent('#keyboard');
			keyboard.showMethod(this.data.showKeyboard);
		}
		if (!this.data.showKeyboard) {
			wx.pageScrollTo({
				scrollTop: 0,
				duration: 400
			});
		}
		if (this.data.numberNoStr.length === 6 && (e.detail.index === 6 || (e.detail.status && e.detail.status === 'end'))) {
			this.setData({
				showKeyboard: false
			});
			let keyboard = this.selectComponent('#keyboard');
			keyboard.showMethod(this.data.showKeyboard);
		}
		this.setData({
			available: this.data.numberNoStr.length === 6
		});
	}
});
